﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;



namespace UI.Controllers
{
    public class HomeController : Controller
    {
        MD5Helper.Md5helps md5 = new MD5Helper.Md5helps();
        ServiceReference1.WebService1SoapClient client = new ServiceReference1.WebService1SoapClient();


        //
        // GET: /Home/
        /// <summary>
        /// 登录页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 注册页面
        /// </summary>
        /// <returns></returns>
        public ActionResult zhuce()
        {
            List<ServiceReference1.Usertab_Role> rolelist = client.showRole().ToList();
            ViewBag.zhucelist = rolelist;
            return View();
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="del"></param>
        /// <returns></returns>
        public int AddUser(ServiceReference1.Usertab_Role del)
        {
            del.userPass = md5.Md5Hash(del.userPass);
            int i = client.AddUser(del);
            return i;
        }
        /// <summary>
        /// 用户正确性判断
        /// </summary>
        /// <param name="del"></param>
        /// <returns></returns>
        public JsonResult usershowpd(ServiceReference1.Usertab_Role del)
        {
            del.userPass = md5.Md5Hash(del.userPass);
            List<ServiceReference1.Usertab_Role> list = client.userShowpd(del).ToList();
            Session["userList"] = list;
            return Json(list);
           
        }
        /// <summary>
        /// 权限显示
        /// </summary>
        /// <returns></returns>
        public JsonResult showquan()
        {
            List<ServiceReference1.Usertab_Role> ulist = Session["userList"] as List<ServiceReference1.Usertab_Role>;

            //List<ServiceReference1.quanXian> quanlist = client.showQuan();
            List<ServiceReference1.quanXian> quanlist = client.chaQuan(ulist[0].RoleId);
            // StringBuilder builder = new StringBuilder();
            string menuHtml = "";
            foreach (var item in quanlist)
            {

                if (item.qfid == 0)
                {
                    menuHtml += "<dl><dt>" + item.qxName + "</dt>";

                    foreach (var child in quanlist)
                    {
                        if (child.qfid == item.qxId)
                        {
                            menuHtml += "<dd><a href=" + child.qxUrl + ">" + child.qxName + "</a></dd>";
                        }
                        else
                        { continue; }
                    }
                    menuHtml += "</dl>";
                }
                else
                {
                    continue;
                }
            }
            return Json(menuHtml);

        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int delQuan(string roleid)
        {
            int i = client.delQuan(roleid);
            return i;
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="qxids"></param>
        /// <returns></returns>
        public int addQuan(int roleid, string qxids)
        {
            int i = client.addQuan(roleid,qxids);
            return i;
        }


        /// <summary>
        /// 登录人员信息
        /// </summary>
        /// <returns></returns>
        public JsonResult renYuanxinxi()
        {
            List<ServiceReference1.Usertab_Role> userList = Session["userList"] as List<ServiceReference1.Usertab_Role>;
            return Json(userList);
        }
        /// <summary>
        /// 用户管理
        /// </summary>
        /// <returns></returns>
        public ActionResult showUser(string userName, int pageIndex = 1)
        {
            int pageSize;
            pageSize = 5;
            int zongCount;
            List<ServiceReference1.Usertab_Role> userShow = client.userShow(pageIndex, pageSize, userName, out  zongCount).ToList();
            ViewBag.zongCount = zongCount;
            ViewBag.userIndex = pageIndex;
            ViewBag.userSize = pageSize;
            ViewBag.usercount = userShow.Count();
            ViewBag.weiyYe = (zongCount + pageSize - 1) / pageSize;
            ViewBag.userShow = userShow;
            List<ServiceReference1.Usertab_Role> rolelistid = client.showRole().ToList();
            ViewBag.rolelist = rolelistid;
            return View();
        }
        /// <summary>
        /// 冻结、解冻用户、删除用户
        /// </summary>
        /// <param name="del"></param>
        /// <returns></returns>
        public ActionResult updUser(int userdel, int usershan, int userid)
        {
            int i = client.userDong(userdel, usershan, userid);
            if (i > 0)
            {
                return Content("<script>location.href='/Home/showUser'</script>");
            }
            else
            {
                return Content("<script>alert('修改失败！');location.href='/Home/showUser'</script>");
            }
        }
        /// <summary>
        /// 查询用户
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public ActionResult updataUser(int userid)
        {

            List<ServiceReference1.Usertab_Role> idlist = client.idShow(userid).ToList();
            List<ServiceReference1.Usertab_Role> chalist = client.showRole().ToList();
            ViewBag.chalist = chalist;
            ViewBag.idlist = idlist;
            return View();
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="del"></param>
        /// <returns></returns>
        public int updateUserDo(ServiceReference1.Usertab_Role del)
        {
            int i = client.delUser(del);
            return i;
        }
        /// <summary>
        /// 角色信息展示
        /// </summary>
        /// <returns></returns>
        public ActionResult showRole()
        {
            List<ServiceReference1.Usertab_Role> rolelist = client.showRole().ToList();
            return View(rolelist);
        }
        /// <summary>
        /// 角色信息查询
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public JsonResult showRoleid(int roleid)
        {
            List<ServiceReference1.Usertab_Role> rolelistid = client.showRole().Where(n => n.RoleId == roleid).ToList();
            return Json(rolelistid);
        }
        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public int updRole(int roleid, string rolename)
        {
            int i = client.updRole(roleid, rolename);
            return i;
        }
        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int delRole(int roleid)
        {
            int i = client.delRole(roleid);
            return i;
        }
        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public int addRole(string rolename)
        {
            int i = client.addRole(rolename);
            return i;
        }
        /// <summary>
        /// 用户权限分配
        /// </summary>
        /// <returns></returns>
        public ActionResult userQuan()
        {
            List<ServiceReference1.Usertab_Role> rolelist = client.showRole().ToList();
            ViewBag.rolelist = rolelist;
            List<ServiceReference1.quanXian> userquan = client.showQuan();
            // ViewBag.userquan = userquan;
            return View(userquan);
        }
        /// <summary>
        /// 机构信息维护页面
        /// </summary>
        /// <returns></returns>
        public ActionResult showJigou()
        {
            List<ServiceReference1.Jigou> jigoulist = client.showJigou().ToList();
            ViewBag.jigoulist = jigoulist;

            return View();
        }
        public ActionResult WeihuJigou(int id)
        {
            List<ServiceReference1.Jigou> IDjigoulist = client.showJigou().Where(n => n.jiGouId == id).ToList();
            ViewBag.IDjigoulist = IDjigoulist;
            return View();
        }
        public ActionResult GengXin(ServiceReference1.Jigou del)
        {
            int i = client.updJigou(del);
            if (i > 0)
            {
                return Content("<script>location.href='/Home/showJigou'</script>");
                }
            else
            {
                return Content("<script>alert('更新失败！');location.href='/Home/showJigou'</script>");
            }
        }
    }
}
